using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Portal;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;

namespace TST.WebParts
{
    public class WebPropertyFilter : ContextFilterWebPart, IWebEditable
    {
        private string _currentSiteProperty;
        private string _parentSiteProperty;
        private string _rootSiteProperty;

        [Browsable(false),
        Personalizable(PersonalizationScope.Shared)]
        public string CurrentSiteProperty
        {
            get { return _currentSiteProperty; }
            set { _currentSiteProperty = value; } 
        }

        [Browsable(false),
        Personalizable(PersonalizationScope.Shared)]
        public string ParentSiteProperty
        {
            get { return _parentSiteProperty; }
            set { _parentSiteProperty = value; }
        }

        [Browsable(false),
        Personalizable(PersonalizationScope.Shared)]
        public string RootSiteProperty
        {
            get { return _rootSiteProperty; }
            set { _rootSiteProperty = value; }
        }

        protected override string DesignTimeHtml
        {
            get
            {
                string result = string.Empty;
                if (string.IsNullOrEmpty(CurrentSiteProperty) && string.IsNullOrEmpty(ParentSiteProperty) && string.IsNullOrEmpty(RootSiteProperty))
                {
                    result = "Please select one of the properties in the web part toolpane.";
                    return result;
                }
                if (!string.IsNullOrEmpty(CurrentSiteProperty))
                {
                    result = string.Format(" This webpart sends the value of '{0}' of the current site to connected webparts.", CurrentSiteProperty);
                }
                if (!string.IsNullOrEmpty(ParentSiteProperty))
                {
                    result += string.Format(" This webpart sends the value of '{0}' of the parent site to connected webparts.", ParentSiteProperty);
                }
                if (!string.IsNullOrEmpty(RootSiteProperty))
                {
                    result += string.Format(" This webpart sends the value of '{0}' of the root site to connected webparts.", RootSiteProperty);
                }
                switch (this.MultipleValueHandling)
                {
                    case FilterMultipleValueHandling.Combine:
                        result += string.Format(" If multiple properties are selected, the values will be combined to 1 single value, separated by a '{0}'", this.ValueSeparator);
                        break;
                    case FilterMultipleValueHandling.SendAll:
                        result += " If multiple properties are selected, all values will be sent.";
                        break;
                    case FilterMultipleValueHandling.SendFirst:
                        result += " If multiple properties are selected, just the first property value will be sent.";
                        break;
                }
                return result;
            }
        }

        protected override string[] GetRawFilterValues()
        {
            
            List<string> values = new List<string>();
            if (!string.IsNullOrEmpty(CurrentSiteProperty))
            {
                string val = SelectWebPropertyEditor.GetValueForProperty(CurrentSiteProperty, SPContext.Current.Web);
                if (!string.IsNullOrEmpty(val))
                {
                    values.Add(val);
                }
            }
            if (!string.IsNullOrEmpty(ParentSiteProperty) && (this.MultipleValueHandling != FilterMultipleValueHandling.SendFirst || values.Count==0))
            {
                using (SPWeb parent = SPContext.Current.Web.ParentWeb)
                {
                    if (parent != null)
                    {
                        string val = SelectWebPropertyEditor.GetValueForProperty(ParentSiteProperty, parent);
                        if (!string.IsNullOrEmpty(val))
                        {
                            values.Add(val);
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(RootSiteProperty) && (this.MultipleValueHandling != FilterMultipleValueHandling.SendFirst || values.Count==0))
            {
                SPWeb root = SPContext.Current.Site.RootWeb;
                try
                {
                    string val = SelectWebPropertyEditor.GetValueForProperty(RootSiteProperty, root);
                    if (!string.IsNullOrEmpty(val))
                    {
                        values.Add(val);
                    }
                }
                finally
                {
                    if (root.ID != SPContext.Current.Web.ID)
                    {
                        root.Dispose();
                    }
                }
            }
            if (values.Count > 0)
            {
                if (values.Count == 1 || MultipleValueHandling == FilterMultipleValueHandling.SendAll)
                {
                    return values.ToArray();
                }
                else if (MultipleValueHandling == FilterMultipleValueHandling.Combine)
                {
                    string result = string.Empty;
                    foreach (string v in values)
                    {
                        result += string.Format("{0}{1}", v, ValueSeparator);
                    }
                    result = result.TrimEnd(ValueSeparator.ToCharArray());
                    return new string[] { result };
                }
            }
            return null;
        }

        public override bool FilterConfigured
        {
            get
            {
                if (!base.FilterConfigured)
                {
                    return false;
                }
                return (!string.IsNullOrEmpty(CurrentSiteProperty) || !string.IsNullOrEmpty(ParentSiteProperty) || !string.IsNullOrEmpty(RootSiteProperty));
            }
        }

        [Browsable(false)]
        public override bool Hidden
        {
            get
            {
                if (base.WebPartManager == null)
                {
                    return base.Hidden;
                }
                if (this.Zone == null)
                {
                    return false;
                }
                return !base.WebPartManager.DisplayMode.AllowPageDesign;
            }
            set
            {
                base.Hidden = value;
            }
        }

        [Browsable(false)]
        public override bool Visible
        {
            get
            {
                if (base.WebPartManager == null)
                {
                    return base.Visible;
                }
                if (this.Zone != null)
                {
                    return true;
                }
                return base.WebPartManager.DisplayMode.AllowPageDesign;
            }
            set
            {
                base.Visible = value;
            }
        }

        #region IWebEditable Members

        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> newEditors = new List<EditorPart>();

            EditorPartCollection editors = base.CreateEditorParts();
            foreach (EditorPart part in editors)
                newEditors.Add(part);

            newEditors.Add(new SelectWebPropertyEditor(this.ID));

            return new EditorPartCollection(newEditors);
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }

        #endregion
    }
}
